// app.js
import { getLoginCode, codeToToken, checkToken, checkSession } from './service/api_login'

App({
  globalData: {
    screenWidth: 0,
    screenHeight: 0,
    // 手机状态栏高度
    statusBarHeight: 0
  },
  onLaunch: function() {
    // 获取设备信息
    const {
      screenWidth,
      screenHeight,
      statusBarHeight
    } = wx.getSystemInfoSync()
    this.globalData.screenWidth =screenWidth
    this.globalData.screenHeight = screenHeight
    this.globalData.statusBarHeight = statusBarHeight

    // 2.让用户默认进行登录
    this.handleLogin()
  },

  handleLogin: async function() {
    const token = wx.getStorageSync('TOKEN-KEY')
    // token有没有过期
    const checkResult = await checkToken()
    // 判断session是否过期
    const isSessionExpire = await checkSession()

    if (!token || checkResult.errorCode || !isSessionExpire) {
      this.loginAction()
    }
  },

  loginAction: async function() {
    // 1.获取code
    const code = await getLoginCode()

    // 2.将code发送给服务器
    const result = await codeToToken(code)
    const token = result.token
    wx.setStorageSync('TOKEN-KEY', token)
  }
})
